﻿
//1，通讯录信息
function loadClient() {
    $('.weui-panel__bd').html("");
    var html = "";
    var followname = "";
    var source;//客户来源
    ajaxGet(siteApiList.ClientList + "?staffId=" + staffId, function (data) {
        if (!isJsonSuccess(data))
            return;

        if (data.Data == null || data.Data.List == null)
            return;
        var list = data.Data.List;
        for (var i = 0; i < list.length; i++) {
            if (list[i].Source == 1) {
                 sourece = "扫码";
            } else if (list[i].Source == 2)
            {
                 sourece = "转发";
            }
            else
                 sourece = "工作交接";
            if (list[i].FollowUpCount == 0)
            {
                followname="今天跟进过";
            }else if (list[i].FollowUpCount < 0) {
                followname="还未跟进过";
            } else { followname = list[i].FollowUpCount + "天前跟进过"; }
            if (list[i].Imgs == "") {
                $('.weui-media-box__thumb').attr('src', '../images/tabbar/17.png')
            }
            html += "<div class='weui-box'><a href='customerDetail.html?clientid=" + list[i].Id + "&staffId=" + list[i].Staffid + "' class='weui-media-box weui-media-box_appmsg'><div class='weui-media-box__hd'><img class='weui-media-box__thumb' src='" + list[i].Imgs + "' alt=''></div><div class='weui-media-box__bd'><h4 class='weui-media-box__title'>" + list[i].UserName + "<span  class='date_right f12'>预计成交率：" + list[i].Probability + "%</span></h4><h4 class='weui-media-box__title'><span class='red-color'>" + followname + "</span><span  class='date_right2 f12'>来源：" + sourece + "</span></h4></div></a></div>"
        }
        $(".weui-panel__bd").append(html);
        $(".totalcount").html(data.Data.PageModel.TotalCount)
    })
}


//2.预计成交率排序和查询
function changeProbability() {
    $('.title_item').click(function () {
        $('.weui-panel__bd').html("");
        var Probability;
        var source;
        if ($(this).text() == "预计成交率") {
            Probability = 1;
        }
        else if ($(this).text() == "最后跟进时间") {
            Probability = 2;
        }
        else if ($(this).text() == "最后活动时间") {
            Probability = 3;
        }
        else if ($(this).text() == "扫码" || $(this).text() == "转发" || $(this).text() == "工作交接") {
            if ($(this).text() == "扫码")
                 source = 1;
            else if ($(this).text() == "转发")
                 source = 2;
            else
                source = 3;
            var html = "";
            ajaxGet(siteApiList.ClientList + "?staffId=" + staffId + "&source=" + source, function (data) {
                if (!isJsonSuccess(data))
                    return;
                else {
                    var list = data.Data.List;
                    if (list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            var sourece1;
                            if (list[i].Source == 1) {
                                sourece1 = "扫码";
                            } else if (list[i].Source == 2)
                            { sourece1 = "转发"; }
                            else {
                                sourece1 = "工作交接";
                            }
                            if (list[i].FollowUpCount == 0) {
                                followname = "今天跟进过";
                            } else if (list[i].FollowUpCount < 0) {
                                followname = "还未跟进过";
                            } else { followname = list[i].FollowUpCount + "天前跟进过"; }
                            if (list[i].Imgs == "") {
                                $('.weui-media-box__thumb').attr('src', '../images/tabbar/17.png')
                            }
                            html += "<div class='weui-box'><a href='customerDetail.html?clientid=" + list[i].Id + "&staffId=" + list[i].Staffid + "' class='weui-media-box weui-media-box_appmsg'><div class='weui-media-box__hd'><img class='weui-media-box__thumb' src='" + list[i].Imgs + "' alt=''></div><div class='weui-media-box__bd'><h4 class='weui-media-box__title'>" + list[i].UserName + "<span  class='date_right f12'>预计成交率：" + list[i].Probability + "%</span></h4><h4 class='weui-media-box__title'><span class='red-color'>" + followname + "<span><span  class='date_right2 f12'>来源：" + sourece1 + "</span></h4></div></a></div>"
                        }
                        $(".weui-panel__bd").append("");
                        $(".weui-panel__bd").append(html);
                    }
                    $(".totalcount").html(data.Data.PageModel.TotalCount);
                }
            })
            return;
        }
        var html = "";
        ajaxGet(siteApiList.ClientList + "?staffId=" + staffId + "&probability=" + Probability, function (data) {
            if (!isJsonSuccess(data))
                return;
            var list = data.Data.List;
            if (list != null) {
                for (var i = 0; i < list.length; i++) {
                    if (list[i].Source == 1)
                        sourece1 = "扫码";
                    else if (list[i].Source == 2)
                        sourece1 = "转发";
                    else
                        sourece1 = "工作交接";
                    if (list[i].FollowUpCount == 0) {
                        followname = "今天跟进过";
                    } else if (list[i].FollowUpCount < 0) {
                        followname = "还未跟进过";
                    } else { followname = list[i].FollowUpCount + "天前跟进过"; }
                    if (list[i].Imgs == "")
                    {
                        $('.weui-media-box__thumb').attr('src', '../images/tabbar/17.png')
                    }
                    html += "<div class='weui-box'><a href='customerDetail.html?clientid=" + list[i].Id + "&staffId=" + list[i].Staffid + "' class='weui-media-box weui-media-box_appmsg'><div class='weui-media-box__hd'><img class='weui-media-box__thumb' src='" + list[i].Imgs + "' alt=''></div><div class='weui-media-box__bd'><h4 class='weui-media-box__title'>" + list[i].UserName + "<span  class='date_right f12'>预计成交率：" + list[i].Probability + "%</span></h4><h4 class='weui-media-box__title'><span class='red-color'>" + followname + "<span><span  class='date_right2 f12'>来源：" + sourece1 + "</span></h4></div></a></div>"
                }
                $('.weui-panel__bd').html("");
                $(".weui-panel__bd").append(html);
            }
            $(".totalcount").html(data.Data.PageModel.TotalCount);
        });
    })
}



//根据客户id得到客户详细信息
function GetCustomerDetail() {
    var clientId = getQueryString("clientid");
    ajaxPost(siteApiList.GetClient, "clientId=" + clientId, function (data) {
        if (!isJsonSuccess(data))
            return;
        var item = data.Data;
        client_uid = item.Uid
        $(".client-logo").attr("src", item.Avater)
        $('.customer-name').text(item.MemoName);
        $('.gailv').text(item.Probability + "%");
        $('.NoContactCount').html(item.NoContactCount);
        $('.FollowUpCount').html(item.FollowUpCount);

        var arr = item.Tags.split(',');
        if (item.Tags == "") {
            $('.text-center').each(function (index, item) {
                if (index <= 2) {
                    $(this).text("添加标签");
                }
            })
        } else {
            for (var i = 0; i < arr.length; i++) {
                if (i < 3) {
                    $('.text-center').eq(i).text(arr[i]);
                    $('.text-center').eq(i).parent().addClass('text-centers');
                    $(".ph-boder").each(function (index, item) {
                        if (index == 0) {
                            if ($(this).children().text() == "添加标签") {
                                $(this).css('background-color', '#f5f3f3');
                            } else {
                                $(this).css('background-color', 'rgb(199, 241, 230)');
                            }

                        }
                        else if (index == 1) {
                            if ($(this).children().text() == "添加标签") {
                                $(this).css('background-color', '#f5f3f3');
                            } else {
                                $(this).css('background-color', 'rgb(192, 224, 249)');
                            }

                        }
                        else {
                            if ($(this).children().text() == "添加标签") {
                                $(this).css('background-color', '#f5f3f3');
                            } else {
                                $(this).css('background-color', 'rgb(243, 215, 179)');
                            }
                        }
                    });
                }
            }
        }
       
    })
}


//设置客户的预计成交时间
function ChangeTradeDate(result) {
    var clientId = getQueryString("clientid");
    ajaxPost(siteApiList.UpdateTradeDate, "clientId=" + clientId + "&updatetime=" + result, function (data) {
        if (!isJsonSuccess(data))
            return;
        GetCustomerDetail();
        toast.show("更改成功!");
    })
}



//设置客户的预计成交概率
function ChangeProbability(result) {
    var clientId = getQueryString("clientid");
    ajaxPost(siteApiList.UpdateProbability, "clientId=" + clientId + "&probability=" + result, function (data) {
        if (!isJsonSuccess(data))
            return;
        GetCustomerDetail();
        toast.show("更改成功!");
    })
}

//获取客户对应标签
function GetTagRelationListById(clientId, staffId) {
    ajaxPost(siteApiList.GetTagRelationListById, "clientId=" + clientId + "&staffId=" + staffId, function (data) {
        if (!isJsonSuccess(data))
            return;
        var list = data.Data.csList;
        if (list == null) return;
        for (var i = 0; i < list.length; i++) {
            if (list[i].CategoryId == 1) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#63f3cf');
                    }
                });
            } else if (list[i].CategoryId == 2) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#63f3cf');
                    }
                });
            } else if (list[i].CategoryId == 3) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#4ba9f1');
                    }
                });
            } else if (list[i].CategoryId == 4) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#4ba9f1');
                    }
                });
            } else if (list[i].CategoryId == 5) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#f6b94d');
                }
                });
            } else if (list[i].CategoryId == 6) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#f6b94d');
                    }
                });
            } else if (list[i].CategoryId == 7) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#4ba9f1');
                    }
                });
            } else if (list[i].CategoryId ==100) {
                $(".ta-center").each(function () {
                    if (list[i].TagName == $(this).text()) {
                        $(this).css('background-color', '#f6b94d');
                    }
                });
            }
        }
        
    })
}

//新增该用户的标签
function AddTagRelation(id, clientId, tagsname) {
    ajaxPost(siteApiList.AddTagRelation, "clientTagid=" + id + "&clientid=" + clientId + "&tagname=" + tagsname, function (data) {
        if (!isJsonSuccess(data))
            return;
        if (data.Data == true) {
            toast.show("更新成功!");
           // window.location.reload();
        }

    })
}    



//添加跟进语
function AddClientFollow() {
    var clientid = getQueryString("clientid");
    var content = $("#content").val();
    if (trim(content) == '') {
        Alert("请填写内容")
        return false;
    }
    ajaxPost(siteApiList.AddClientFollow, "clientId=" + clientid + "&staffId=" + staffId + "&content=" + content, function (data) {
        if (!isJsonSuccess(data))
            return;
        if (data.Data == true) {
            toast.show("添加成功!");
            window.location.href = "customerDetail.html?clientid=" + clientid + "&staffId=" + staffId+"&followstate=1";
        }

    })
}


//获取跟进语列表
function GetClientFollowList(pageObj) {
    var html = "";
    var clientid = getQueryString("clientid");
    if (pageObj == null) pageObj = { num: 1 };
    ajaxGet(siteApiList.GetClientFollowListPages + "?staffId=" + staffId + "&clientid=" + clientid + "&page=" + pageObj.num, function (data) {
        if (!isJsonSuccess(data))
            return;

        
        
        if (isNoData(data.Data.FollowList)) {
            $('.follow_lists').append(getNoDataMsg(pageObj.num));
            mescrollFollow.endSuccess(0);  //设置条数
            return;
        }
        mescrollFollow.endSuccess(data.Data.FollowList.length);  //设置条数
        if (data.Data.FollowList == null)
            return;
        var list = data.Data.FollowList;
        for (var i = 0; i < list.length; i++) {
            html += "<div class='weui-cells'><a class='weui-cell weui-cell_access' href='javascript:;'><div class='weui-cell__bd'><p style='margin-left:10px;'>" + list[i].Content + "</p></div><span class='sptime'>-" + list[i].AddTime + "</span></a></div>"
        }
        $(".follow_lists").append(html);

    })
}
